﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using NLog;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OddUserManager.Web.Controllers
{

    public class BookMarkController : BaseController
    {
        static Logger log = LogManager.GetCurrentClassLogger();
        private readonly IBookMarkService _service;

        public BookMarkController(IBookMarkService service) {
            _service = service;
        }

        // GET: Menus
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetAllList() {
            JsonData d = new JsonData();
            d.Data = _service.GetAllList();
            return Json(d);
        }


        public IActionResult GetAllPageList(int startPage, int pageSize)
        {

            JsonData data = new JsonData();
            try
            {
                PageViewCollection<BookMark> pageList = _service.SearchMarkPages(null,startPage,pageSize,null,null);

                data.Success = true;
                data.Message = "查询成功";
                data.Data = pageList;

            }
            catch (Exception ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }


        public IActionResult Get(int id)
        {
            JsonData d = new JsonData();
            d.Success = true;
            d.Data = _service.Get(id);
            return Json(d);
        }

        public async Task<IActionResult> Edit(BookMark dto)
        {
            JsonData d = new JsonData();
            try
            {
                BookMark bookMark = dto;
                if (dto == null) {
                    d.Success = false;
                    d.Message = "参数为空";
                    return Json(d);
                }

                var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                if (!result.Succeeded)
                {
                    d.Success = false;
                    d.Message = "用户已失效";
                    return Json(d);
                }
                var appUserId = result.Principal.Identity.Name;
                log.Info($"用户：{appUserId}执行编辑操作");
                
                if (dto.Id < 1)   // 新增
                {
                    bookMark.CreateDate = DateTime.Now;
                    bookMark.CreateAppUserId = int.Parse(appUserId);
                }
                else {
                    bookMark = _service.Get(dto.Id);
                    bookMark.MarkColor = dto.MarkColor;
                    bookMark.MarkDesc = dto.MarkDesc;
                    bookMark.MarkIndex = dto.MarkIndex;
                    bookMark.MarkName = dto.MarkName;
                }

                if (bookMark.MarkIndex < 1)   // 默认排序加载排序最大值加1
                {  
                    var list = _service.GetListOrderByIndex();
                    if (list == null || list.Count < 1)
                    {
                        bookMark.MarkIndex = 1;
                    }
                    else
                    {
                        bookMark.MarkIndex = list[0].MarkIndex + 1;
                    }
                }

                bookMark.ModDate = DateTime.Now;
                bookMark.ModAppUserId = int.Parse(appUserId);
                _service.UpdateOrInsert(bookMark);
                d.Success = true;
                d.Message = "编辑成功";
            }
            catch (Exception ex)
            {
                d.Success = false;
                d.Message = ex.Message;
                log.Error(ex,ex.Message);
            }
            return Json(d);
        }

        public async Task<IActionResult> DeleteMuti(string ids)
        {
            JsonData d = new JsonData();

            try
            {

                var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                if (!result.Succeeded)
                {
                    d.Success = false;
                    d.Message = "用户已失效";
                    return Json(d);
                }
                var appUserId = result.Principal.Identity.Name;
                log.Error($"用户：{appUserId}执行删除操作，删除{ids}");

                string[] idArray = ids.Split(',');

                List<int> delIds = new List<int>();
                foreach (string id in idArray)
                {
                    delIds.Add(int.Parse(id));
                }
                _service.DeleteBatch(delIds);
                d.Success = true;
                d.Message = "删除成功";

            }
            catch (Exception ex)
            {
                d.Success = false;
                d.Message = ex.Message;
                log.Error(ex,ex.Message);
            }
            return Json(d);
        }


    }
}
